import Vue from "vue";
import App from "./App.vue";
import router from "./router";

// element
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
Vue.use(ElementUI);
// axops
import axios from "axios";
axios.defaults.baseURL = "http://liangwei.tech:3000";
axios.interceptors.request.use(config => {
  const token = localStorage.getItem("token");
  if (token) {
    config.headers["Authorization"] = token;
  }
  return config;
});
Vue.prototype.$axios = axios;
Vue.config.productionTip = false;

// router
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem("token");
  if (to.path != "/login") {
    if (!token) {
      router.push({ path: "/login" }).catch();
      return;
    }
  }

  next();
});

new Vue({
  router,
  render: function(h) {
    return h(App);
  }
}).$mount("#app");
